package com.mybatis.demo;

import com.mybatis.mapper.TestMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2018/4/10.
 */
public class Demo {

    public static SqlSession getSqlSession() throws FileNotFoundException {
        String filePath = Demo.class.getResource("").getPath() + "mybatis-config.xml";
        //配置文件
        InputStream configFile = new FileInputStream(filePath);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile);
        //加载配置文件得到SqlSessionFactory
        return sqlSessionFactory.openSession();
    }

    public static void main(String[] args) {
        try {
            TestMapper mapper = getSqlSession().getMapper(TestMapper.class);
            System.out.println(mapper.selectByPrimaryKey(1));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
